﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class Edge
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerMove : MonoBehaviour {

    //移动速度
    public float speed = 8f;

    public Edge edge;

    public float tiit =  4f;

    //发射器点
    public Transform bulletSmitter;

    //子弹预设
    public GameObject bulletPreb;


    //刚体组件
    private Rigidbody _rigidbody;

    private float fFireRate = .25f;

    private float fNextFire = 0f;

    void Start()
    {
        _rigidbody = this.GetComponent<Rigidbody>();

        if (null == _rigidbody)
        {
            Debug.LogError("there is not Rigidbody!!");
        }
    }

    void FixedUpdate()
    {
        float hTranslate = Input.GetAxis("Horizontal");
        float vTranslate = Input.GetAxis("Vertical");

        Vector3 moveMent = new Vector3(hTranslate, 0f, vTranslate);

        _rigidbody.velocity = moveMent *speed;

        _rigidbody.position = new Vector3
        (
            Mathf.Clamp(_rigidbody.position.x, edge.xMin, edge.xMax),
            0f,
            Mathf.Clamp(_rigidbody.position.z, edge.zMin, edge.zMax)
        );

        _rigidbody.rotation = Quaternion.Euler(0f, 0f, _rigidbody.velocity.x * -tiit);

    }

    public void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > fNextFire)
        {
            fNextFire = Time.time + fFireRate;
            GameObject go = Instantiate(bulletPreb, bulletSmitter.position, bulletSmitter.rotation) as GameObject;
        }

    }

    
}

